/**
 * websocket工具类
 * @author hugan
 * @date 2023/10/9
 */
export default {
  isConnected(websocket) {
    return websocket && websocket.readyState === WebSocket.OPEN
  },
  /**
   * 按[本项目]规定的格式,序列化data,并发送
   * @param websocket [nullable,未连接]时没任何操作
   * @param path notEmpty
   * @param data nullable,字符串,或对象
   */
  send(websocket, path, data) {
    if (!websocket || websocket.readyState !== WebSocket.OPEN) return

    if (!path || path.length > 127) {
      throw new Error(`path异常[${path}],要求长度[1,127]`)
    }

    //数字, 字符串, 对象
    let dataStr;
    if (!data && false !== data && 0 !== data) {
      dataStr = ''
    } else if ('string' === typeof data) {
      dataStr = data
    } else {
      // console.log('stringify:', typeof data, data)
      dataStr = JSON.stringify(data)
    }

    // console.log('dataStr', `[${data}]->[${dataStr}], ${typeof dataStr}`)
    dataStr = path + dataStr
    // console.log('dataStr2', `[${dataStr}]`)
    let temp = new TextEncoder().encode(dataStr)
    let toSend = new Uint8Array(temp.length + 1)
    toSend[0] = path.length
    toSend.set(temp, 1)

    websocket.send(toSend)
  },
  handleMessage(event, handler) {
    // console.log("handleMessage", event)
    if (typeof event.data === 'string') {
      let resp = JSON.parse(event.data)
      if (handler[resp.handlerMethod]) {
        handler[resp.handlerMethod](resp.data)
        return
      }
      console.warn('未知消息', resp)
    } else {
      console.warn('未知消息', event)
    }
  },
  //===
  /**
   * 测试本类的send方法
   */
  testClient() {
    let mock = {
      readyState: WebSocket.OPEN,
      send: (bytes) => {
        console.log('send', bytes)
      }
    }
    this.send(mock, 'p', 1)
    this.send(mock, 'p', 1 / 0)
    this.send(mock, 'p', 0)
    this.send(mock, 'p', false)
    this.send(mock, 'p', null)
    this.send(mock, 'p', "")
    this.send(mock, 'p')
    this.send(mock, 'p', "aa")
    this.send(mock, 'p', {n: "aa"})
  },
  /**
   * 测试服务端接口
   */
  testServer(websocket) {
    // 字符串,等
    websocket.send("abb")
    websocket.send(123)
    websocket.send(new Uint8Array(10))
    //
    let toSend
    //请求体异常
    toSend = new Uint8Array(10)
    toSend[0] = 10
    websocket.send(toSend)
    //path匹配异常
    toSend = new Uint8Array(10)
    toSend[0] = 9
    websocket.send(toSend)
    // this.send(websocket, 'wsLogin')
    //非json数据,
    // this.send(websocket, 'wsLogin', 'as')
    // this.send(websocket, 'wsLogin', 1)
    this.send(websocket, 'int', 1)
    // this.send(websocket, 'int', null)
    // this.send(websocket, 'int2', "falsex")
    // this.send(websocket, 'int2', null)
    //
    this.send(websocket, 'str', null)
    this.send(websocket, 'str', "null")
    this.send(websocket, 'str', "")
    // this.send(websocket, 'double', "")
    // this.send(websocket, 'double')
    this.send(websocket, 'double', 1)
    this.send(websocket, 'double', 1.2)
    this.send(websocket, 'float', 1.2)
    this.send(websocket, 'notReq', 1.2)
    this.send(websocket, 'notReq', null)
    this.send(websocket, 'notReq2')
  }
}